package com.diandireader.net;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

/**
 * deal with the network connection
 * @author mayh
 *
 */
public class BookMarketClient {
	
	public final static String BASEURL = "http://192.168.0.88:8080/DiandiReaderServer";//the server base url
	public final static String BESTBOOK = "/bookWebService/bookOfBest.json";//list the best books
	public final static String CATEGORY = "/bookWebService/bookCategory.json";//list the book category
	public final static String BOOKBYCATEGORY = "/bookWebService/bookByCategory.json";//list the book by category
	public final static String BOOKMOSTREAD = "/bookWebService/bookMostRead.json";//list the book most read recently
	
	public final static int SUCCESS_BESTBOOK = 0;
	public final static int SUCCESS_BOOKCATEGORY = 1;
	public final static int SUCCESS_BOOKBYCATEGORY = 2;
	public final static int SUCCESS_BOOKMOSTREAD = 3;
	public final static int ERROR = -1;
	public final static int TIMEOUT = -2;
	
	//load data from server
	public static String loadData(String url){
		String dataStr = "";
		try {
		HttpGet httprequest = new HttpGet(url);
		HttpClient client = new DefaultHttpClient();
			HttpResponse response = client.execute(httprequest);
			if(response.getStatusLine().getStatusCode()==HttpStatus.SC_OK){
				dataStr = EntityUtils.toString(response.getEntity());
			}else {
				dataStr = "TIMEOUT";
			}
			
		} catch (ClientProtocolException e) {
			e.printStackTrace();
			dataStr = "ERROR";
		} catch (IOException e) {
			e.printStackTrace();
			dataStr = "ERROR";
		}
		return dataStr;
	}
	
	//send use get method
	public static void sendByGet(){
		//http connection url object
		HttpGet httprequest = new HttpGet(BASEURL+BESTBOOK);
		//init the http client
		HttpClient client = new DefaultHttpClient();
		try {
			HttpResponse response = client.execute(httprequest);
			if(response.getStatusLine().getStatusCode()==HttpStatus.SC_OK){
				String result = EntityUtils.toString(response.getEntity());
				System.out.println("result"+result);
			}
			
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	//send through the post menthod
	public static void sendByPost(){
		HttpPost postRequest = new HttpPost(BASEURL+BESTBOOK);
		HttpClient client = new DefaultHttpClient();
		List<NameValuePair> paramList = new ArrayList<NameValuePair>();
		//set the data to transfer
		try {
			HttpEntity entity = new UrlEncodedFormEntity(paramList,"UTF-8");
			postRequest.setEntity(entity);
			HttpResponse response = client.execute(postRequest);
			if(response.getStatusLine().getStatusCode()==HttpStatus.SC_OK){
				String result = EntityUtils.toString(response.getEntity());
				System.out.println("result"+result);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	//send by URLConnection 
	public static void sendByURLConnectionGet(){
		try {
			URL url = new URL(BASEURL+BESTBOOK);
			HttpURLConnection connection = (HttpURLConnection)url.openConnection();
			connection.setRequestMethod("GET");
			connection.setUseCaches(true);
			BufferedReader bis = new BufferedReader(new InputStreamReader(connection.getInputStream()));
			String readLine = null;
			String data = null;
			while((readLine=bis.readLine())!=null){
				
			}
			bis.close();
			connection.disconnect();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	//send by URLConnection 
	public static void sendByURLConnectionPost(){
		try {
			URL url = new URL(BASEURL+BESTBOOK);
			HttpURLConnection connection = (HttpURLConnection)url.openConnection();
			connection.setDoInput(true);
			connection.setDoOutput(true);
			connection.setRequestMethod("POST");
			connection.setUseCaches(false);
			connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded"); 
			connection.setInstanceFollowRedirects(true);
			connection.connect();
			//write data
			
			//read data
			BufferedReader bis = new BufferedReader(new InputStreamReader(connection.getInputStream()));
			String readLine = null;
			String data = null;
			while((readLine=bis.readLine())!=null){
				
			}
			bis.close();
			connection.disconnect();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
